<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-20
 * Func   彩票种类
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery');

if(IS_POST)
{
    $id   = intval($_REQUEST['id']);
// 	!$id && AjaxReturn(['error'=>'1','msg'=>'参数丢失']);
    $data = $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id);

    //  表单数据
    $post = $_POST['data'];

    $ins = [];
    $ins['title']	=   NoBadStr($post['title']);
    $ins['thumb']	=   NoBadStr($post['thumb']);
    $ins['description']	= $post['description'];
    $ins['content']   	= $post['content']   ;
    $ins['recommend']=	intval($post['recommend']);
    $ins['type']	=	intval($post['type']);
    $ins['sales']	=	intval($post['sales']);
    //$ins['credit']	=	intval($post['credit']);
    //$ins['lotteryid']=	intval($post['lotteryid']);
    //$ins['ptid']	=	NoBadStr($post['ptid']);
    $ins['cate_id']     = $post['cate_id'];
    $ins['per_explain'] = $post['per_explain'];
    $ins['sname']       = $post['sname'];
    $ins['periods']     = $post['periods'];
    $ins['description'] = addslashes(htmlspecialchars_decode($ins['description']));
    $ins['content']     = addslashes(htmlspecialchars_decode($ins['content']));
    
    // 查询彩种类别信息，写入lottery中
    $cateInfo           = $db->getrow('SELECT sname FROM ' . Table('lottery_category') . ' WHERE id = ' . $post['cate_id']);
    $ins['ptid']        = $cateInfo['sname'];
    $ins['status']      = $post['status']; // 默认可用
    $ins['credit']      = $post['credit']; // 默认官方，信用为自营
    if($data['id']) {
        $flag = $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
    }else{
        $flag = $db->exec('INSERT INTO '.$table.' SET '.CreateUpdateSql($ins));
    }
    
    if ($flag){
        ShowMsg('操作成功',U('lottery/index'));
    }else{
        ShowMsg('操作失败',-1);
        // AjaxReturn(['error'=>'1','msg'=>'操作失败']);
    }
}

if($a == 'add'){
    // 载入所有的menu，并且设置好树形结构
    $menuTreeData = getMenuTree($db);
    $id = intval($_GET['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];
	!$data['id'] && ShowMsg('彩种不存在',-1);
    $tpl->display($m.'/index_add');
}
else if ($a == 'operate')
{
    if($_GET['recommend'])
	{
        count($_GET['ids']) && $db->exec('UPDATE '.Table('lottery').' SET recommend = 1 WHERE id IN ('.implode(',',$_GET['ids']).')');
		
        ShowMsg('推荐成功',-1);
    }
	else if($_GET['canclerecommend'])
	{
        count($_GET['ids']) && $db->exec('UPDATE '.Table('lottery').' SET recommend = 0 WHERE id IN ('.implode(',',$_GET['ids']).')');
		
        ShowMsg('取消成功',-1);
    }
}else if ($a == 'add_lott'){
    $menuTreeData = getMenuTree($db);
    $tpl->display($m.'/index_add');
}else{
    $cateIdKey = getCateIdKey($db);
    $lottList  = $db->getall('SELECT id, title, cate_id FROM ' . Table('lottery') . ' WHERE status = 1');
    $data  = array();
    $where = ' FROM '.$table.' t WHERE 1 ';

    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    if($tmp['p']) {
        $page     = max(1,intval($_GET['page']));
        $size     = 20;
        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
        $orderby  = ' ORDER BY t.id ASC ';
        $url      = '?m='.$m.'&c='.$c.'&a='.$a;
        $data     = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		$multi    = Multi($url,$page,$tmp['p'],$size);
    }
    // 查询信用彩种开奖类型
    $creditTypeSql  = 'SELECT id, title FROM ' . Table('lottery_credit_type') . ' WHERE status = 0';
    $creditTypeList = $db->getall($creditTypeSql);
    // 查询所有信用彩种设置的开奖方式
    $creditPlaySql  = 'SELECT id, lott_id, type_id, open_param FROM ' . Table('lottery_credit_play') . ' WHERE status = 0';
    $creditPlayList = $db->getall($creditPlaySql);
    $tpl->display($m.'/index');
}

function getCateIdKey ($db){
    $idKey      = array();
    $getCateSql = 'SELECT * FROM ' . Table('lottery_category');
    $cateList   = $db->getall($getCateSql);
    foreach ($cateList as $k => $v){
        $idKey[$v['id']] = $v;
    }
    return $idKey;
}

function getMenuTree($db){
    // 查询所有的栏目信息，
    $sqlGetMenu = 'SELECT * FROM ' . Table('lottery_category') . ' ORDER BY id ASC';
    $menuList   = $db->getall($sqlGetMenu);
    $menuIdKey  = array();
    foreach ($menuList as $mk => $mv){
        $menuIdKey[$mv['id']] = $mv;
    }
    // menu信息准备生成树状结构
    // 递归获取级联数据
    $arr              = array();
    foreach ($menuList as $k => $v){
        if ($v['pid'] == 0){
            $v['lv'] = '1';
            $arr[]   = $v;
            getChild($menuList, $v['id'], $arr, 1);
        }
    }
    return $arr;
}

function getChild($menuList, $id, &$arr, $lv){
    $lv++;
    foreach ($menuList as $k => $v){
        if ($v['pid'] == $id){
            $v['lv']   = $lv;
            $line      = '';
            for($i = 1; $i < $lv; $i++){
                $line  = $line . '－－';
            }
            $v['line'] = $line;
            $arr[]     = $v;
            // 检查是否还有子项
            getChild($menuList, $v['id'], $arr, $lv);
        }
    }
}

